package org.example.stuview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HandleStudent implements ActionListener {
    StudentView studentView;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;
    @Override
    public void actionPerformed(ActionEvent e) {
        InetAddress address= null;
        socket=new Socket();
        thread=new Thread(studentView);
        try {
            address = InetAddress.getByName("127.0.0.1");
            InetSocketAddress socketAddress=new InetSocketAddress
                    (address,4331);
            socket.connect(socketAddress);
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            thread.start();
        } catch (IOException ex) {
            socket=new Socket();
        }

        if (e.getSource() == studentView.test){
            TestView testView = new TestView();
            testView.stuID = studentView.stuID;
            testView.stuName = studentView.stuName;
            studentView.setVisible(false);
            testView.init();
            testView.testListener();
            try {
                out.writeUTF("开始考试发题");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else if (e.getSource() == studentView.lookScore) {
            try {
                out.writeUTF("查询考生历史成绩" + studentView.stuID);
                System.out.println("查询考生历史成绩" + studentView.stuID);
                studentView.tableDate.setRowCount(0);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

    }

    public void setStudentView(StudentView studentView) {
        this.studentView = studentView;
    }
}
